﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudyDemo.Delegate;
 
public class DelegateExample
{
    /*
     调用示例：
     CalcFunc func = DelegateExample.Add;
     func+=DelegateExample.Mul;
     func.Invoke(5, 4);
//func(2, 3);
     */

    public delegate float CalcFunc(float a, float b);

    public static string Add(float a, float b)
    {
        /*
         Func<float, float,string> func = DelegateExample.Add;
        Console.WriteLine(func(2.5f, 3.5f).ToString());
         */

        Console.WriteLine($"{a}+{b}={a + b}");
        return (a + b).ToString();
    }

    public static float Sub(float a, float b)
    {
        /*
        Func<float, float, float> func2 = DelegateExample.Mul;
        func2+=DelegateExample.Sub;
        func2(2f, 3f);
         */
        Console.WriteLine($"{a}-{b}={a - b}");
        return a - b;
    }
    public static float Mul(float a, float b)
    {
        Console.WriteLine($"{a}*{b}={a * b}");
        return a * b;
    }

    public static void Print(string info)=> Console.WriteLine(info);

    /// <summary>
    /// 闭包示例
    /// </summary>
    public static void Closure()
    {

        #region 闭包
        int i = 0;
        Action closure = () =>
        {
            i += 10;
        };
        closure();
        Console.WriteLine(i);
        #endregion
    }
}
